//Copyright 2012 - Jonathan Allen

#if SILVERLIGHT && !WINDOWS_PHONE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using System.Diagnostics.Contracts;
using System.Diagnostics.CodeAnalysis;
using System.Windows.Browser;

namespace Granite.Xaml.Internals
{

	/// <summary>
	/// This INavigator is used for Silverlight applications that use a Frame for navigation
	/// </summary>
	public class FrameNavigator : INavigator
	{
		readonly Frame m_Frame;

		/// <summary>
		/// Creates a new NavigationServiceNavigator
		/// </summary>
		/// <param name="frame"></param>
		public FrameNavigator(Frame frame)
		{
			if (frame == null)
				throw new ArgumentNullException("frame", "frame is null.");
			Contract.EndContractBlock();

			m_Frame = frame;
		}

		/// <summary>
		/// Navigate to the indicated URI
		/// </summary>
		/// <param name="target"></param>
		public virtual void Navigate(Uri target)
		{
			Frame.Navigate(target);

		}

		/// <summary>
		/// Indicates that GoBack can be safely called.
		/// </summary>
		public virtual bool CanGoBack
		{
			get { return Frame.CanGoBack; }
		}

		/// <summary>
		/// Indicates that GoForward can be safely called.
		/// </summary>
		public virtual bool CanGoForward
		{
			get { return Frame.CanGoForward; }
		}

		/// <summary>
		/// Return to the previous View in the navigation journal.
		/// </summary>
		public virtual void GoBack()
		{
			Frame.GoBack();
		}

		/// <summary>
		/// Go to the next view in the navigation journal.
		/// </summary>
		public virtual void GoForward()
		{
			Frame.GoForward();
		}

		/// <summary>
		/// Access the underlying Frame for this Navigator
		/// </summary>
		public Frame Frame
		{
			get
			{
				Contract.Ensures(Contract.Result<Frame>() != null);
				return m_Frame;
			}
		}

		[SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic"), SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
		[ContractInvariantMethod]
		void ContractInvariant()
		{
			Contract.Invariant(m_Frame != null);
		}


		/// <summary>
		/// Opens a website.
		/// </summary>
		/// <param name="target"></param>
		public void OpenWebsite(Uri target)
		{
			if (target == null)
				throw new ArgumentNullException("target", "target is null.");
			Contract.EndContractBlock();

			Contract.Assume(!string.IsNullOrEmpty(target.ToString()));
			HtmlPage.Window.Navigate(target, "_newWindow", "toolbar=1,menubar=1,resizable=1,scrollbars=1,top=0,left=0");

		}

		/// <summary>
		/// Indicates whether or not websites are included in the journal
		/// </summary>
		public bool IncludeWebInNavigation
		{
			get { return false; }
		}
	}

}
#endif



